//
//  ShowViewController.m
//  MaxMovie
//
//  Created by Maxdong on 14-12-24.
//  Copyright (c) 2014年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "ShowViewController.h"
#import "ShowModel.h"
#import "NetworkHandler.h"
#import "SlideImageView.h"
#import "SDWebImageManager.h"
#import "WillNRViewController.h"
#import "WillViewController.h"
#import "SetViewController.h"
#import "loginViewController.h"
@interface ShowViewController ()<SlideImageViewDelegate>

@property (nonatomic, retain) SlideImageView *slideImageView;
@property (nonatomic, retain) ShowModel *showModel;
@property (nonatomic, retain) NSArray *imageArray;

@end

@implementation ShowViewController

- (void)dealloc
{
    [super dealloc];
    [_slideImageView release];
    [_showModel release];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
      
    }
    return self;
}

- (void)loadView
{
    [super loadView];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"our.png"]];
    
    // Do any additional setup after loading the view.
    _showModel = [ShowModel new];
    
    NSString *str = [NSString stringWithFormat:@"http://api.douban.com/v2/movie/nowplaying?app_name=doubanmovie&client=e:iPhone4,1|y:iPhoneOS_6.1|s:mobile|f:doubanmovie_2|v:3.3.1|m:PP_market|udid:aa1b815b8a4d1e961347304e74b9f9593d95e1c5&alt=json&version=2&start=1&city=北京&apikey=0df993c66c0c636e29ecbb5344252a4a"];
    
    [NetworkHandler connectioonNetGETDataWithURLString:str completion:^(id result)
     {
         
         [self.showModel setValuesForKeysWithDictionary:result];
         NSMutableArray *tmpImageArrray = [[NSMutableArray alloc] init];
         for (int i = 0; i < self.showModel.entries.count; i++)
         {
             NSString *tempImgStr = [[[self.showModel.entries objectAtIndex:i] objectForKey:@"images"] objectForKey:@"large"];
             SDWebImageManager *manager = [SDWebImageManager sharedManager];
             
             UIImage *cachedImage = [manager imageWithURL:[NSURL URLWithString:tempImgStr]];
             
             if (cachedImage)
             {
                 // Use the cached image immediatly
             }
             else
             {
                 // Start an async download
                 [manager downloadWithURL:[NSURL URLWithString:tempImgStr] delegate:self];
                 
             }
             if (cachedImage)
             {
                 [tmpImageArrray addObject:cachedImage];
             }
             
         }
         self.imageArray = tmpImageArrray;
         
         [tmpImageArrray release];
         
         [self setImage];
     }];
    
    // 设置slideImageView
    SlideImageView *tmpSlideImageView = [[SlideImageView alloc]initWithFrame:CGRectMake(50, 150, 200, 250) ZMarginValue:5 XMarginValue:10 AngleValue:0.3 Alpha:1000];
    tmpSlideImageView.borderColor = [UIColor whiteColor];
    tmpSlideImageView.delegate = self;
    [self.view addSubview:tmpSlideImageView];
    [tmpSlideImageView release];
    self.slideImageView = tmpSlideImageView;
    [self.slideImageView reLoadUIview];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 420, 320, 30)];
    titleLabel.tag = 101;
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [titleLabel setTextColor:[UIColor yellowColor]];
    [self.view addSubview:titleLabel];
    [titleLabel release];
    
    UILabel *ratingLabel = [[UILabel alloc]initWithFrame:CGRectMake(240, 440, 100, 30)];
    ratingLabel.tag = 102;
    ratingLabel.font = [UIFont boldSystemFontOfSize:14];
    [ratingLabel setTextColor:[UIColor colorWithRed:255.0 / 255.0 green:215.0 / 255.0 blue:0.0 / 255.0 alpha:2.0]];
    [self.view addSubview:ratingLabel];
    [ratingLabel release];
    
    UILabel *pubdateLabel = [[UILabel alloc]initWithFrame:CGRectMake(55, 470, 150, 30)];
    pubdateLabel.tag = 103;
    [pubdateLabel setTextColor:[UIColor colorWithRed:123.0 / 255.0 green:104.0 / 255.0 blue:238.0 / 255.0 alpha:2.0]];
    pubdateLabel.font = [ UIFont boldSystemFontOfSize:15];
    [self.view addSubview:pubdateLabel];
    [pubdateLabel release];
    
    UILabel *wishLabel = [[UILabel alloc]initWithFrame:CGRectMake(190, 470, 170, 30)];
    wishLabel.tag = 104;
    [wishLabel setTextColor:[UIColor colorWithRed:123.0 / 255.0 green:104.0 / 255.0 blue:238.0 / 255.0 alpha:2.0]];
    wishLabel.font = [UIFont boldSystemFontOfSize:15];
    [self.view addSubview:wishLabel];
    [wishLabel release];
    
    
    
    //左右button
    
    UIButton * leftb =[UIButton buttonWithType:UIButtonTypeSystem];
    leftb.frame =CGRectMake(20, 20, 20, 20);
    UIImage * img1 =[UIImage imageNamed:@"login.png"];
    [leftb setBackgroundImage:img1 forState:UIControlStateNormal];
    [leftb addTarget:self action:@selector(leftbAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:leftb];
    //[leftb release];
    
    
    UIButton * rightb =[UIButton buttonWithType:UIButtonTypeSystem];
    rightb.frame =CGRectMake(280, 20, 20, 20);
    UIImage * img =[UIImage imageNamed:@"chouti.png"];
    [rightb setBackgroundImage:img forState:UIControlStateNormal];
    [rightb addTarget:self action:@selector(rightbAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:rightb];
    //[rightb release];
    
}


- (void)leftbAction:(id)sender
{
    
    loginViewController * loginVC =[[loginViewController alloc] init];
//    UINavigationController *loginNavi = [[[UINavigationController alloc]initWithRootViewController:loginVC]autorelease];
    [self presentViewController:loginVC animated:YES completion:^{
        
    }];
    [loginVC release];

    
}
- (void)rightbAction:(id)sender
{
//    WillNRViewController * willnr=[[WillNRViewController alloc] init];
    WillViewController * will =[[WillViewController alloc] init];
    [self.navigationController pushViewController:will animated:YES];
}
- (void)setImage
{
    for (UIImage *tmpImage in self.imageArray)
    {
        [self.slideImageView addImage:tmpImage];
    }
    [self.slideImageView reLoadUIview];
    UILabel *titleLabel = (UILabel *)[self.view viewWithTag:101];
    NSString *str0 = [NSString stringWithFormat:@"✨%@✨",[[self.showModel.entries objectAtIndex:0] objectForKey:@"title"]];
    titleLabel.text = str0;
    
    UILabel *ratingLabel = (UILabel *)[self.view viewWithTag:102];
    NSString *str1 = [NSString stringWithFormat:@"%@分",[[self.showModel.entries objectAtIndex:0] objectForKey:@"rating"]];
    ratingLabel.text = str1;
    
    UILabel *pubdateLabel = (UILabel *)[self.view viewWithTag:103];
    NSString *str2 = [NSString stringWithFormat:@"%@上映",[[self.showModel.entries objectAtIndex:0]objectForKey:@"pubdate"]];
    pubdateLabel.text = str2;
    
    UILabel *wishLabel = (UILabel *)[self.view viewWithTag:104];
    NSString *str3 = [NSString stringWithFormat:@"%@人想看",[[self.showModel.entries objectAtIndex:0]objectForKey:@"wish"]];
    wishLabel.text = str3;
    
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)SlideImageViewDidClickWithIndex:(int)index
{
    NSString *indexStr = [[NSString alloc]initWithFormat:@"点击了第%d张",index];
    //    clickLabel.text = indexStr;
    [indexStr release];
}

- (void)SlideImageViewDidEndScorllWithIndex:(int)index
{
    NSString *indexStr = [[NSString alloc]initWithFormat:@"当前为第%d张",index];
    //    indexLabel.text = indexStr;
    
    UILabel *titleLabel = (UILabel *)[self.view viewWithTag:101];
    NSString *str0 = [NSString stringWithFormat:@"✨%@✨",[[self.showModel.entries objectAtIndex:index] objectForKey:@"title"]];
    titleLabel.text = str0;
    
    UILabel *ratingLabel = (UILabel *)[self.view viewWithTag:102];
    NSString *str1 = [NSString stringWithFormat:@"%@分",[[self.showModel.entries objectAtIndex:index]objectForKey:@"rating"]];
    ratingLabel.text = str1;
    
    UILabel *pubdateLabel = (UILabel *)[self.view viewWithTag:103];
    NSString *str2 = [NSString stringWithFormat:@"%@上映",[[self.showModel.entries objectAtIndex:index]objectForKey:@"pubdate"]];
    pubdateLabel.text = str2;
    
    UILabel *wishLabel = (UILabel *)[self.view viewWithTag:104];
    NSString *str3 = [NSString stringWithFormat:@"%@人想看",[[self.showModel.entries objectAtIndex:index]objectForKey:@"wish"]];
    wishLabel.text = str3;
    
    [indexStr release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
